<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/11
 * Time: 10:54
 */
namespace Service\Controller;
use Admin\Controller\AdminController;

class CenterController extends AdminController
{
    public function index(){
        $req = I('param.');
        $tel = $req['tel'];
        $tel = preg_replace('/(0*)(1[^0])(\d{9})/','$2$3',$tel);
        $where['attestation_tel'] = $tel;
        $where['tel'] = $tel;
        $where['_logic'] = 'or';
//        myDump($tel);die;
        $len = strlen($tel);
        if($len == 11)
            $data_tel = D('Customer')->where($where)->relation(array('families'))->select();
        else
            $data_tel = D('Customer')->where(array('phone'=>$tel))->relation(array('families'))->select();
        $data_contact = D('Customer')->where(array('contact'=>array('like',"%$tel%")))->relation(array('families'))->limit(10)->select();

        if($data_tel){
            $data_tel[0]['primary'] = 1;
        }
        $data = array_merge($data_tel,$data_contact);

        $wxTel = $data[0]['attestation_tel']?$data[0]['attestation_tel']:$data[0]['tel'];
        $wxuser = D('Admin/WxuserInfo')->where(array('tel'=>$wxTel))->find();
//        dd($wxuser);
        if($wxuser)
            $order = D('Service/WxOrder')->selectStep($type=false, $step=false, $pageSize=99, $id=false, $wxuser['wecha_id'])['data'];
        elseif($tel)
            $order = D('Service/WxOrder')->selectStep($type=false, $step=false, $pageSize=99, $id=false, false, false, $tel)['data'];
        if($tel)
            $phoneOrder = D('Service/Order')->getLst(999,$tel)['data'];
        //-----------------------------------家庭其他成员------------

        $family_id = M('FamilyMember')->where(['customer_id'=>$data[0]['id']])->find()['family_id'];
        if($family_id) {
            $members = M('FamilyMember')->where(['family_id' => $family_id, 'customer_id' => ['neq', $data[0]['id']]])->select();
//        myDump($members);die;
            foreach ($members as $member) {
                $cus = M('Customer')->find($member['customer_id']);
                $wxTel = $cus['attestation_tel'] ? $cus['attestation_tel'] : $cus['tel'];
                $wxuser = D('Admin/WxuserInfo')->where(array('tel' => $wxTel))->find();
//        dd($wxuser);
                if ($wxuser)
                    $order1 = D('Service/WxOrder')->selectStep($type = false, $step = false, $pageSize = 99, $id = false, $wxuser['wecha_id'])['data'];
                elseif ($wxTel)
                    $order1 = D('Service/WxOrder')->selectStep($type = false, $step = false, $pageSize = 99, $id = false, false, false, $wxTel)['data'];
                if ($wxTel)
                    $phoneOrder1 = D('Service/Order')->getLst(999, $wxTel)['data'];
//            myDump($member);
//            myDump($order1);
                $order = array_merge($order, $order1);
                $phoneOrder = array_merge($phoneOrder, $phoneOrder1);
            }
        }
//        myDump($order);die;
//        myDump($data);
//        myDump($tel);
//        myDump($wxuser);
//        myDump($order);die;
        //是否具有维修分类权限
        $typeManage = power_confirm();
//        myDump($typeManage);
        //右上角 订单计数
        $count['1'] = D('WxOrder')->countStep(1);
//        $count['2'] = D('WxOrder')->countStep('3,4');
        $this->assign('count', $count);

        $this->assign('data',$data);
        $this->assign('phoneOrder',$phoneOrder);
        $this->assign('order',$order);
        $this->assign('typeManage',$typeManage);
        $this->assign('user',getUser());
        $this->display();
    }

    public function choice(){
        $req = I('param.');
        if($req['tel']){
            $tel = $req['tel'];
            $tel = preg_replace('/(0*)(1[^0])(\d{9})/','$2$3',$tel);
            $contact = array('like','%'.$req['tel'].'%');
            $where['tel'] = $tel;
            $where['attestation_tel'] = $tel;
            $where['contact'] = $contact;
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        if($req['name']){
            $name = array('like','%'.$req['name'].'%');
            $map['name'] = $name;
        }
        if($req['phone']){
            $phone = array('like','%'.$req['phone'].'%');
            $map['phone'] = $phone;
        }
        if($map)
            $customer = D('Admin/Customer')->where($map)->relation(array('families'))->select();
        $this->assign('customer',$customer);
        $this->assign('user',getUser());
//        dump($customer);
        $this->display();
    }

    public function getCustomer(){
        $req = I('param.');
        if($req['id'] == 'all'){
            $data = D('Admin/Customer')->relation(true)->select();
            $count = D('Admin/Customer')->count();
            $res['stat'] = $data ? 1 : 0;
            $res['msg'] = $data ? '找到'.$count.'条数据' : '没有找到数据';
            $res['data'] = $data;
        }elseif($req['id']){
            $where['id'] = $req['id'];
            $data = D('Admin/Customer')->where($where)->relation(true)->find();
            $res['stat'] = $data ? 1 : 0;
            $res['msg'] = $data ? '找到1条数据' : '没有找到数据';
            $res['data'] = $data;
        }else{
            $res['stat'] = 0;
            $res['msg'] = '参数提交不正确';
        }
        foreach(D('Admin/Dict')->getDict('ugroup') as $k=>$v){
            $ugroup[$v['value']] = $v;
        }
        $this->assign('data',$res['data']);
        $this->assign('ugroup',$ugroup);
        // dump($ugroup);
        // dump($data);
        $this->display();
    }

    public function edit(){
        $customer_id = I('param.')['id'];
        $customer = D('Admin/Customer')->relation(true)->find($customer_id);
        $this->assign('customer',$customer);
        $this->display();
    }


}